package com.bana.fvs.protocol;


import com.bana.sys.protocol.Protocol;
import com.bana.sys.utils.Convert;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CollectSocket extends Protocol {

    @Override
    public short getId()
    {
        return 0x0001;
    }

    public List<SocketItem> handle(byte[] buffer) throws IOException {

        List<SocketItem> sockets=new ArrayList<>();
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        DataInputStream dis = new DataInputStream(bais);
        int size=dis.readShort();
        for(int i=0;i<size;++i)
        {
            SocketItem item = new SocketItem();
            int controllerId=dis.readInt();
            String remoteIp=Convert.toIp(buffer,dis);
            int remotePort=Short.toUnsignedInt(dis.readShort());
            String localIp=Convert.toIp(buffer,dis);
            int localPort=Short.toUnsignedInt(dis.readShort());

            item.setRemoteAddress(remoteIp + ":" + remotePort);
            item.setLocalAddress(localIp + ":" + localPort);
            item.setTag(String.format("%d_%d",localPort,controllerId));
            item.setTransmit(dis.readLong());
            item.setReceive(dis.readLong());
            sockets.add(item);
        }

        dis.close();
        bais.close();

        return sockets;
    }

    public class SocketItem {

        private String localAddress;
        private String remoteAddress;
        private String tag;
        private int type;
        private long transmit;
        private long receive;

        public String getRemoteAddress() {
            return remoteAddress;
        }

        public void setRemoteAddress(String remoteAddress) {
            this.remoteAddress = remoteAddress;
        }

        public String getLocalAddress() {
            return localAddress;
        }

        public void setLocalAddress(String localAddress) {
            this.localAddress = localAddress;
        }

        public String getTag() {
            return tag;
        }

        public void setTag(String tag) {
            this.tag = tag;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public long getTransmit() {
            return transmit;
        }

        public void setTransmit(long transmit) {
            this.transmit = transmit;
        }

        public long getReceive() {
            return receive;
        }

        public void setReceive(long receive) {
            this.receive = receive;
        }
    }


}
